package com.borya.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.text.MessageFormat;
import java.util.Properties;

import org.apache.log4j.Logger;

public class PropertyUtils {

	private Logger log = Logger.getLogger(getClass());
	// "GBK"
	public static final String CHARSET = "UTF-8";
	protected Properties prop;
	protected String path;

	public PropertyUtils init(String path, Class<?> cl) {
		this.path = path;
		prop = load(path, cl);
		return this;
	}

	/**
	 * 
	 * @param cl
	 *            类的原型
	 * @param file_relative_path
	 *            文件相对项目的路�? eg: /conf/conf.properties
	 * @return
	 */
	protected Properties load(String path, Class<?> cl) {
		if (cl == null || path == null) {
			return null;
		}
		return load(cl.getResourceAsStream(path));
	}

	public Properties getProp() {
		return prop;
	}

	protected Properties load(InputStream in) {
		try {
			BufferedReader buffer = new BufferedReader(new InputStreamReader(in, "utf-8"));
			Properties prop = new Properties();
			prop.load(buffer);
			in.close();
			return prop;
		} catch (Exception e) {
			log.error("Load " + path + " error:" + e.getMessage());
		}
		return null;
	}

	// /**
	// * 读取properties的全部信�?
	// */
	// protected void readAllProperties() {
	// try {
	// Enumeration<?> en = prop.propertyNames();
	// while (en.hasMoreElements()) {
	// String key = (String) en.nextElement();
	// String Property = prop.getProperty(key);
	// System.out.println(key + Property);
	// }
	// } catch (Exception e) {
	// log.error(e.getMessage(), e);
	// }
	// }

	public String getString(String propertyName) {
		return getProperty(propertyName);
	}

	public String getProperty(String propertyName) {
		try {
			String val = prop.getProperty(propertyName);
			if (val == null) {
				return "";
			}
			return new String(val.getBytes(), CHARSET);
		} catch (UnsupportedEncodingException e) {
			log.error(e.getMessage(), e);
		}
		return "";
	}

	/**
	 * 获取配置文件的url类型的属性�?,并以"\"结尾
	 * 
	 * @param propertyName
	 * @return
	 */
	protected String getURL(String propertyName) {
		String str = getString(propertyName);
		if (!str.endsWith(File.separator)) {
			str += File.separator;
		}
		return str;
	}

	public int getInteger(String propertyName) {
		return Integer.parseInt(getProperty(propertyName));
	}

	public long getLong(String propertyName) {
		return Long.parseLong(getProperty(propertyName));
	}

	protected boolean setProperty(String propertyName, String propertyValue) {
		try {
			String name = this.getClass().getResource("/").getPath() + path;
			OutputStream outputStream = new FileOutputStream(name);
			prop.setProperty(propertyName, propertyValue);
			prop.store(outputStream, "");
			outputStream.close();
		} catch (Exception ex) {
			log.error(ex.getMessage(), ex);
		}
		return false;
	}

	/**
	 * 格式化字符串 eg: str = "我是{0},我来自{1},今年{2}�?; format(str, "中国�?,"北京",22));
	 * 
	 * @param pattern
	 * @param arguments
	 * @return
	 */
	public String format(String pattern, Object... arguments) {
		return MessageFormat.format(pattern, arguments);
	}

}
